/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.itintegration.catalogue.mek.ejb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MekHandler extends DefaultHandler {

    private Map<String, String> hsaToKombikaMap = new HashMap<>();
    private String kombika;
    private String hsaId;
    private boolean bKombika;
    private boolean bHsaId;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {

        if (qName.equalsIgnoreCase("mappning")) {
        } else if (qName.equalsIgnoreCase("HSAId")) {
            bHsaId = true;
        } else if (qName.equalsIgnoreCase("Kombikakod")) {
            bKombika = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("mappning")) {
            hsaToKombikaMap.put(hsaId, kombika);
        }
    }

    @Override
    public void characters(char ch[], int start, int length) throws SAXException {
        if (bKombika) {
            kombika = new String(ch, start, length);
            bKombika = false;
        } else if (bHsaId) {
            hsaId = new String(ch, start, length);
            bHsaId = false;
        }
    }

    public Map<String, String> getMap() {
        return hsaToKombikaMap;
    }
}
